package  
{
	import flash.display.Sprite;
	
	import flash.net.*;
	
	import flash.events.*;
		
	import flash.utils.*;	
	
	
	import flash.events.EventDispatcher;
	
	public class Connection extends  Sprite
	{
		private var xmlSocket:XMLSocket;
		private var port:uint = 999;
		private const ATTRIBUTE_SEPERATOR:String = "|";
		private var pingTimer:Timer;
		private var connTimer:Timer;
		private var logined:Boolean = false;
		private var mainClass:Main;
	//	private var sockUrl:String = 'localhost';
		private var sockUrl:String = '178.215.92.3';
		
		public function Connection(mainClass:Main)
		{
			this.mainClass = mainClass;
			this.mainClass.addText("Подключение к серверу...<br/>");
			if (pingTimer == null)
            {
                pingTimer = new Timer(1000, 0);
                pingTimer.addEventListener(flash.events.TimerEvent.TIMER, onPing);
                onPing(null);
                pingTimer.start();
            }
		}
		
		public function connect():void {
			if (xmlSocket != null)
            {
                xmlSocket.removeEventListener(flash.events.Event.CONNECT, onConnection);
                xmlSocket.removeEventListener(flash.events.IOErrorEvent.IO_ERROR, handleIOError);
                xmlSocket.removeEventListener(flash.events.DataEvent.DATA, onDataHandler);
                xmlSocket.removeEventListener(flash.events.Event.CLOSE, onConnectionLost);
                if (xmlSocket.connected)
                {
                    xmlSocket.close();
                }
            }
            xmlSocket = new flash.net.XMLSocket();
            xmlSocket.addEventListener(flash.events.Event.CONNECT, onConnection);
            xmlSocket.addEventListener(flash.events.IOErrorEvent.IO_ERROR, handleIOError);
            xmlSocket.addEventListener(flash.events.DataEvent.DATA, onDataHandler);
            xmlSocket.addEventListener(flash.events.Event.CLOSE, onConnectionLost);
			xmlSocket.addEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            xmlSocket.connect(sockUrl, port);
            return;
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void {
            //this.mainClass.addText("securityErrorHandler: " + event);
        }
		
		public function onConnection(arg1:flash.events.Event):void
        {
			this.mainClass.showField();
			sendCommand("login|" + mainClass.first_name + " " + mainClass.last_name + "|" + mainClass.userID );
		}
		
		public function handleIOError(arg1:flash.events.Event):void
        {
            //this.mainClass.addText("handleIOError");
            return;
        }
		
		private function onDataHandler(arg1:flash.events.DataEvent):void
        {
			this.mainClass.addText(arg1.data);
		}
		
		private function onConnectionLost(arg1:flash.events.Event):void
        {
            this.mainClass.addText("Потеряна связь с сервером<br/>");
			this.mainClass.hideField();
			//connTimer = new Timer(1000, 0);
			//connTimer.addEventListener(flash.events.TimerEvent.TIMER, reconnect);
			//connTimer.start();
        }
		
		public function sendCommand(arg1:String, arg2:Array=null):void
        {
            if (!xmlSocket.connected)
            {
                return;
            }
            var loc1:*;
            loc1 = arg1;
            xmlSocket.send(loc1);
            return;
        }
		
		private function onPing(arg1:flash.events.TimerEvent):void
        {
			if (xmlSocket == null || !xmlSocket.connected)
            {
				this.connect();
                return;
            }
            sendCommand("!ping");
			if(!logined) {
				logined = true;
			}
            return;
        }

	}

}